#include <ros/ros.h>
#include "executor/chassis_executor.h"
#include "executor/gimbal_executor.h"
#include "behavior_tree/behavior_node.h"
#include "behavior_tree/behavior_tree.h"
#include "example_behavior/aim_behavior.h"
#include "example_behavior/back_boot_area_behavior.h"
#include "example_behavior/chase_behavior.h"
#include "example_behavior/circle_behavior.h"
#include "example_behavior/dance_behavior.h"
#include "example_behavior/escape_behavior.h"
#include "example_behavior/gimbal_aim_behavior.h"
#include "example_behavior/gimbalget0_behavior.h"
#include "example_behavior/goal_behavior.h"
#include "example_behavior/gogoal_behavior.h"
#include "example_behavior/patrol_behavior.h"
#include "example_behavior/search_behavior.h"
#include "example_behavior/shake_behavior.h"
#include "example_behavior/wait_behavior.h"
#include "example_behavior/under_attack_behavior.h"
#include "example_behavior/bfs_search.h"


void param_init(ros::NodeHandle& p){}

int main(int argc, char **argv){
    ros::init(argc, argv, "behavior_test_node");
	std::string full_path = ros::package::getPath("roborts_decision") + "/config/decision.prototxt";
	ros::NodeHandle nh_p("~") ;
	param_init(nh_p);
    auto blackboard_ptr_ = std::make_shared<roborts_decision::Blackboard>(full_path);
    roborts_decision::Blackboard* blackboard = &*blackboard_ptr_;
    auto bfs_search = new roborts_decision::BfsSearch(blackboard);
    auto chassis_executor_ptr_ = std::make_shared<roborts_decision::ChassisExecutor>();
    chassis_executor_ptr_->Set_Search(bfs_search);
    auto gimbal_executor_ptr_ = std::make_shared<roborts_decision::GimbalExecutor>();

    auto shake_action_=std::make_shared<roborts_decision::ShakeAction>(blackboard_ptr_, gimbal_executor_ptr_);
    auto dance_action_=std::make_shared<roborts_decision::DanceAction>(blackboard_ptr_, chassis_executor_ptr_, gimbal_executor_ptr_);
    auto patrol_action_ = std::make_shared<roborts_decision::PatrolAction>(blackboard_ptr_, chassis_executor_ptr_);
    auto chase_action_=std::make_shared<roborts_decision::ChaseAction>(blackboard_ptr_,chassis_executor_ptr_);
    auto under_attack_action_ =std::make_shared<roborts_decision::UnderAttackAction>(blackboard_ptr_, chassis_executor_ptr_);

    auto patrol_parallel_=std::make_shared<roborts_decision::ParallelNode>("patrol parallel", blackboard_ptr_, 1);
    patrol_parallel_->AddChildren(patrol_action_);
    patrol_parallel_->AddChildren(shake_action_);

    auto under_attack_condition_=std::make_shared<roborts_decision::PreconditionNode>("under attack condition", blackboard_ptr_, [&](){
        if(blackboard_ptr_->IsUnderAttack())return true;
        else return false;
    }, roborts_decision::AbortType::BOTH);
    under_attack_condition_->SetChild(under_attack_action_);

    auto far_enemy_condition_=std::make_shared<roborts_decision::PreconditionNode>("far enemy condition", blackboard_ptr_, [&](){
        if(blackboard_ptr_->GetIfEnemy().data&&
        blackboard_ptr_->GetDistance(blackboard_ptr_->GetRobotMapPose(), blackboard_ptr_->GetAimPosition())>2)return true;
        else return false;
    }, roborts_decision::AbortType::BOTH);
    far_enemy_condition_->SetChild(chase_action_);

    auto get_enemy_condition_=std::make_shared<roborts_decision::PreconditionNode>("get enemy condition", blackboard_ptr_, [&](){
        if(blackboard_ptr_->GetIfEnemy().data)return true;
        else return false;
    }, roborts_decision::AbortType::BOTH);
    get_enemy_condition_->SetChild(dance_action_);
    
    auto root_selector_ = std::make_shared<roborts_decision::SelectorNode>("root",blackboard_ptr_);
    root_selector_->AddChildren(under_attack_condition_);
    root_selector_->AddChildren(far_enemy_condition_);
    root_selector_->AddChildren(get_enemy_condition_);
    root_selector_->AddChildren(patrol_parallel_);
    roborts_decision::BehaviorTree root(root_selector_,100);
    sleep(3);
    root.Run();
    return 0;
}